import * as $api from '../api/index'

async function bindGetUserInfo() {
	let params = {
		code: "",
		raw_data: "",
		signature: "",
	}
	await wx.login({
		success: res => {
			params.code = res.code;
		}
	})
	let res = ''
	try {
		res = await wx.getUserProfile({
			lang: "zh_CN",
			desc: '用于展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写

		})
	} catch (e) {
		return
	}
	wx.showLoading({
		title: '正在登录'
	})
	console.log("res",res)
	params.raw_data = res.rawData;
	params.signature = res.signature;
	const result = await $api.wxAppLogin(params);
	if (result.data.level == 'success') {
		wx.setStorageSync('userToken', result.data.data);
		wx.showToast({
			title: "登录成功",
			icon: "none",
			duration: 1500
		})
		wx.hideLoading();
	} else {
		wx.hideLoading();
		wx.showToast({
			title: result.data.message,
			icon: "none",
			duration: 1500
		})
	}

}

//获取个人信息
async function getUserInfos() {
	let userToken = uni.getStorageSync('userToken');
	const res = await $api.getUserInfos({
		user_token: userToken
	});
	if (res.data.level == 'success') {
		uni.setStorageSync('UserId', res.data.data.id);
		uni.setStorageSync('is_Vip', res.data.data.user_info.is_member_unit); //判断是否单位入驻
		uni.setStorageSync('is_unit_auth', res.data.data.user_info.is_unit_auth);
		uni.setStorageSync('is_personal_auth', res.data.data.user_info.personal_auth_status);
	}
	wx.hideLoading()
}


export default {
	bindGetUserInfo,
	getUserInfos
}
